package grisar.gris.tower;

import java.util.List;
import java.util.ListIterator;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import grisar.gris.helpers.DataHolder;
import grisar.gris.helpers.PhotoShop;
import grisar.gris.struct.I_Bounding;
import grisar.gris.struct.I_Drawable;
import grisar.gris.struct.I_GameObject;
import grisar.gris.struct.I_Tower;
import grisar.gris.struct.I_Updateable;
import grisar.gris.struct.bound.BoundCircle;
import grisar.gris.struct.enemy.I_Enemy;
import grisar.gris.helpers.Geometrics;

public class LaserTower implements I_Tower,I_GameObject, I_Updateable, I_Drawable{

	private Point pos;
	private Paint paint = new Paint();
	private I_Bounding bound;
	private Point drawOffset;
	private Bitmap bitmap;
	
	private int range = 150;
	
	private int laserX;
	private int laserY;
	private boolean drawLaser = false;
	int i = 0;
	int k = 0;
	
	public LaserTower(Point pos, Bitmap bitmap){
		
		//Bitmap mBackground = BitmapFactory.decodeResource(drawable.image);
		
		this.pos=pos;
		paint.setColor(Color.CYAN);
		paint.setStrokeWidth( (float) 3.4 );
		this.bitmap = bitmap;
		drawOffset = new Point(-bitmap.getWidth()/2,-bitmap.getHeight()+20);
		//TODO MAke radius dynamix
		bound = new BoundCircle(this, 30);
		
	}
	
	//@Override
	//Skjuter av laser efter 1000 ticks.
	public void update() {
		// TODO Auto-generated method stub
	
		if(i!=10){
			i++;
		}
		else{
			fire();
			i = 0;
		}

		
	}
	//S�ker fiende och skjuter laser.
	private void fire() {
		// TODO Auto-generated method stub
		ListIterator<I_Enemy> enemies  = DataHolder.getEnemies().listIterator();
		while(enemies.hasNext()){
			I_Enemy en = enemies.next();
			Point enemyPoint = en.getPosition();
			if(Geometrics.distance(enemyPoint , pos) < range){
				laserX = enemyPoint.x + 50;
				laserY = enemyPoint.y + 50;
//				en.reduceHP(10);
				drawLaser = true;
				break;
			}
		}
	}

	//@Override
	public void draw(Canvas canvas) {
		
		PhotoShop.draw(canvas, bitmap, pos);
		
		if(drawLaser){
			
			canvas.drawLine(pos.x + 28, pos.y + 2, laserX, laserY, paint);
			if(k!=5){
				k++;
			}
			else{
				drawLaser = false;
				k=0;
			}
		}
	}

	//@Override
	public Point getPosition() {

		return pos;
	}

	//@Override
	public void setPosition(Point pos) {
		
		this.pos=pos;
	}

	//@Override
	public int getPrice() {
		// TODO Auto-generated method stub
		return 0;
	}

	//@Override
	public int sell() {
		// TODO Auto-generated method stub
		return 0;
	}

	//@Override
	public int uppgradePrice() {
		// TODO Auto-generated method stub
		return 0;
	}

	//@Override
	public int uppgradeTower() {
		// TODO Auto-generated method stub
		return 0;
	}

	//@Override
	public boolean isColliding(I_Bounding bound) {

		return this.bound.isColliding(bound.getBounding());
	}

	//@Override
	public I_Bounding getBounding() {
		
		return bound;
	}



	
}
